package gentle.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import gentle.base.BaseController;
import gentle.entity.*;
import gentle.factory.PageFactory;
import gentle.service.IConsultantDetailsService;
import gentle.service.IConsultantService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("consultant")
public class ConsultantController extends BaseController {

    @Autowired
    IConsultantService consultantService;
    @Autowired
    IConsultantDetailsService consultantDetailsService;

    /**
     * 列表加载tc看诊
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchConsultantInfo info, HttpServletRequest request) {
        if (info.getAbbreviation() == "" || info.getAbbreviation() == null || info.getAbbreviation() == "null") {
            return null;
        } else {
            Page<ConsultantInfo> page = new PageFactory<ConsultantInfo>().defaultPage();
            try {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<ConsultantInfo> list = consultantService.selectPageListByQueryGetMap(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = consultantService.selectPageListByQueryGetMap(page, info);
                }
                page.setTotal(consultantService.getQueryPagesCount(page, info));
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    /**
     * 数量统计查询tc看诊 
     */
    @RequestMapping(value = "/totalTc", method = RequestMethod.GET)
    @ResponseBody
    public List<ConsultantInfo> totalTc(String param1, String param2, String param3, String param4, String param5,
                                        String param6, String param7, String param8, String param9, String abbreviation) throws Exception {
        return consultantService.totalTc(param1,param2,param3,param4,param5,param6,param7,param8,param9,abbreviation);
    }

    @RequestMapping(value = "/getTotleMoneys", method = RequestMethod.POST)
    @ResponseBody
    public Double getTotleMoneys(String id,String abbreviation) throws Exception {
        return consultantService.getTotleMoneys(id,abbreviation);
    }

    //患者信息
    @RequestMapping(value = "/patientInformation", method = RequestMethod.POST)
    @ResponseBody
    public CustomerInfo patientInformation(String id,String abbreviation,String areaAbbreviation) {
        CustomerInfo customerInfo = consultantService.selectOneById(id,abbreviation); //当前记录的患者信息
        FirstCustomerInfo firstCustomerInfo = consultantService.firstVisitInformation(customerInfo.getReservationsId(),abbreviation); //首次到店的患者信息
        customerInfo.setOrthodonticNumber(consultantService.getorthodonticNumber(customerInfo.getReservationsId(),abbreviation,areaAbbreviation));
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        return customerInfo;
    }

    //就诊记录
    @RequestMapping(value = "/visitRecords", method = RequestMethod.POST)
    @ResponseBody
    public List<CustomerInfo> visitRecords(String reservationsId,String abbreviation,String outpatientName) {
        List<CustomerInfo> customerInfoList = consultantService.visitRecords(reservationsId,abbreviation,outpatientName);
        return customerInfoList;
    }

    //通过 ID 查看患者信息是否成交
    @RequestMapping(value = "/getCustomerIsDealStatus", method = RequestMethod.POST)
    @ResponseBody
    public int getCustomerIsDealStatus(String customerId,String abbreviation, Model model) {
        return consultantService.getCustomerIsDealStatus(customerId,abbreviation);
    }

    //tc开单操作
    @RequestMapping(value = "/orderOperation", method = RequestMethod.POST)
    @ResponseBody
    public ScoreDealInfo orderOperation(String reservationsId, String abbreviation, String proportional, Integer lowestPriceBill,String areaAbbreviation) {
        ScoreDealInfo scoreDealInfo = consultantDetailsService.orderOperation(reservationsId);
        scoreDealInfo.setMoneyBalance(consultantDetailsService.getMoneyBalance(reservationsId));
        scoreDealInfo.setJfbalance(consultantDetailsService.getJfbalance(reservationsId,proportional));
        scoreDealInfo.setLowestPriceBill(lowestPriceBill);
        VipLevelInfo vipLevelInfo = consultantDetailsService.getVipLevel(reservationsId,abbreviation);
        scoreDealInfo.setVipLevelName(vipLevelInfo.getVipLevelName());
        scoreDealInfo.setDiscountDiscount(vipLevelInfo.getDiscountDiscount());
        scoreDealInfo.setGetDepartmentList(consultantDetailsService.getDepartmentList(areaAbbreviation));
        scoreDealInfo.setGetNurseList(consultantDetailsService.getNurseList(abbreviation));
        scoreDealInfo.setGetDoctorList(consultantDetailsService.getDoctorList(abbreviation));
        scoreDealInfo.setObtainDeductionMethod(consultantDetailsService.getObtainDeductionMethod());
        return scoreDealInfo;
    }

    //开单记录
    @RequestMapping(value = "/orderDealListTc", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> orderDealListTc(String reservationsId, String abbreviation, String areaAbbreviation) {
        List<PayServicedInfo> payServiceInfoListS = consultantDetailsService.orderDealListTc(reservationsId,abbreviation,areaAbbreviation);
        return payServiceInfoListS;
    }

    //搜索项目
    @RequestMapping(value = "/payServiceSearch", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> payServiceSearch(String reach, String reservationsId,String treatmentUnit,String abbreviation,String areaAbbreviation) {
        return consultantService.payServiceSearch(reach,reservationsId,treatmentUnit,abbreviation,areaAbbreviation);
    }

    //看诊/回访
    @RequestMapping(value = "/diagnoseTc", method = RequestMethod.POST)
    @ResponseBody
    public ConsultantInfo diagnoseTc(String reservationsId,String abbreviation,String userName) {
        ConsultantInfo infoList = consultantDetailsService.diagnoseTc(reservationsId);
        infoList.setGetTcList(consultantDetailsService.getTcList(abbreviation));
        infoList.setGetSfList(consultantDetailsService.getSfList(userName));
        infoList.setGetJgList(consultantDetailsService.getJgList(userName));
        return infoList;
    }

    //获得影像信息
    @RequestMapping(path = "/getIconInfo", method = RequestMethod.POST)
    @ResponseBody
    public List<IconInfo> getIconInfo(String cri,String abbreviation,String areaAbbreviation) {
        return consultantService.getIconInfo(cri,abbreviation,areaAbbreviation);
    }

    //病历
    @RequestMapping(value = "/medicalRecord", method = RequestMethod.POST)
    @ResponseBody
    public List<CaseInfo> medicalRecord(String reservationsId, String abbreviation,String areaAbbreviation) {
        return consultantService.medicalRecord(reservationsId,abbreviation,areaAbbreviation);
    }

    //账单记录
    @RequestMapping(value = "/chargingRecord", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo billingRecords(String reservationsId, String abbreviation) {
        return consultantService.billingRecords(reservationsId);
    }

    //支付记录
    @RequestMapping(value = "/paymentRecords", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo paymentRecords(String reservationsId, String abbreviation) {
        return consultantService.paymentRecords(reservationsId);
    }

    //欠费记录
    @RequestMapping(value = "/arrearageRecord", method = RequestMethod.POST)
    @ResponseBody
    public ArrearageRecordInfo arrearageRecord(String reservationsId, String abbreviation,String proportional,String areaAbbreviation) {
        ArrearageRecordInfo arrearageRecordInfo = consultantService.arrearageRecord(reservationsId,abbreviation,proportional,areaAbbreviation);
        String checkoutMethodHtml = consultantService.getCheckoutMethodList();
        List<TcInfo> tcList = consultantService.getTcList(abbreviation);
        arrearageRecordInfo.setTcList(tcList);
        arrearageRecordInfo.setCheckoutMethodHtml(checkoutMethodHtml);
        return arrearageRecordInfo;
    }

    //会员信息
    @RequestMapping(value = "/memberInformation", method = RequestMethod.POST)
    @ResponseBody
    public ScoreDealInfo memberInformation(String reservationsId,String abbreviation,String proportional) {
        return consultantService.memberInformation(reservationsId,abbreviation,proportional);
    }

    //得到开单的状态
    @RequestMapping(value = "/getDealStatus", method = RequestMethod.POST)
    @ResponseBody
    public int getDealStatus(String reservationsId, String operationTime, String customerId, String abbreviation) {
        return consultantService.getDealStatus(reservationsId,operationTime,customerId,abbreviation);
    }

    //图片删除
    @RequestMapping(value = "/imgDel", method = RequestMethod.POST)
    @ResponseBody
    public String imgDel(String id,String abbreviation,String areaAbbreviation,String userName,String userIdValue,String outpatientName) {
        return consultantService.imgDel(id,abbreviation,areaAbbreviation,userName,userIdValue,outpatientName);
    }

    //得到一级项目
    @RequestMapping(value = "/getFirstLevelProject", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> getFirstLevelProject(String customerId,String treatmentUnit,String abbreviation,String areaAbbreviation) throws Exception {
        return consultantService.getFirstLevelProject(customerId,treatmentUnit,abbreviation,areaAbbreviation);
    }

    //得到项目名称
    @RequestMapping(value = "/getPaySeriveName", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> getPaySeriveName(String department,String customerId,String firstLevelProject,String abbreviation,String areaAbbreviation) throws Exception {
        return consultantService.getPaySeriveName(department,customerId,firstLevelProject,areaAbbreviation);
    }

    //得到免费的项目
    @RequestMapping(value = "/getPaySeriveNameFreeService", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> getPaySeriveNameFreeService(String vipLevelName,String areaAbbreviation) throws Exception {
        return consultantService.getPaySeriveNameFreeService(vipLevelName,areaAbbreviation);
    }

    //得到优惠抵扣的金额
    @RequestMapping(value = "/getCouponPrice", method = RequestMethod.POST)
    @ResponseBody
    public Double getCouponPrice(String id) {
        return consultantService.getCouponPrice(id);
    }

    //得到客户状态
    @RequestMapping(value = "/getCustomerStatus", method = RequestMethod.POST)
    @ResponseBody
    public String getCustomerStatus(String customerId,String abbreviation,String userName,String roleName,Integer dealyes,Integer isRefundAdmin) {
        return consultantService.getCustomerStatus(customerId,abbreviation,userName,roleName,dealyes,isRefundAdmin);
    }

    //项目详情
    @RequestMapping(value = "/projectDetail", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject projectDetail(String id,String areaAbbreviation) {
        PayServiceInfo payServiceInfo = consultantService.projectDetail(id,areaAbbreviation);
        return JSONObject.fromObject(payServiceInfo);
    }

    //开单操作
    @RequestMapping(value = "/openBillNew", method = RequestMethod.POST)
    @ResponseBody
    public String openBillNew(@RequestParam("list") String list) throws Exception {
        //jackson对象
        ObjectMapper mapper = new ObjectMapper();
        //使用jackson将json转为List<?>
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, PaymentInfo.class);
        List<PaymentInfo> paymentInfoList = (List<PaymentInfo>) mapper.readValue(list, jt);
        String openBillNew = consultantService.openBillNew(paymentInfoList);
        return openBillNew;
    }

    //修改开单
    @RequestMapping(value = "/updateDealInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updateDealInfo(@RequestParam("list") String list) throws Exception {
        //jackson对象
        ObjectMapper mapper = new ObjectMapper();
        //使用jackson将json转为List<?>
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, PaymentInfo.class);
        List<PaymentInfo> paymentInfoList = (List<PaymentInfo>) mapper.readValue(list, jt);
        String openBillNew = consultantService.updateDealInfo(paymentInfoList);
        return openBillNew;
    }

    // 医生/TC添加看诊记录
    @RequestMapping(value = "/add_diagnosisContent1", method = RequestMethod.POST)
    @ResponseBody
    public int add_diagnosisContent1(String id, String reservationsId, String diagnosisContent,String abbreviation, String userName, String outpatientName) throws Exception {
        return consultantService.add_diagnosisContent1(id,reservationsId,diagnosisContent,abbreviation,userName,outpatientName);
    }

    //整单补欠款
    @RequestMapping(value = "/repairArrears")
    @ResponseBody
    public int repairArrears(String operationTimes, Double arrearsAmount, String reservationsId, String arrearsRemark, String tcName, String customerId,
                             String arrearsOutpatientName,String abbreviation,String outpatientName) {
        return consultantService.repairArrears(operationTimes, arrearsAmount, reservationsId, arrearsRemark, tcName, customerId, arrearsOutpatientName,abbreviation,outpatientName);
    }

    //补欠款
    @RequestMapping(value = "/repairArrearsYh")
    @ResponseBody
    public int repairArrearsYh(@RequestBody List<PaymentInfo> list, Model model) throws Exception {
        return consultantService.repairArrearsYh(list);
    }

}
